package com.app.web.money.job;

import com.app.utils.base.j2se.Common;
import com.app.utils.base.j2se.Convert;
import com.app.web.user.login.LoginInfo;
import com.app.web.user.login.LoginService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 退货打款任务
 * Created by Wang on 2020/3/22.
 */
@Component
public class CancelMoney implements Job {
    @Autowired
    private CancelMoneyService cancelMoneyService;
    @Autowired
    private LoginService loginService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 查询退货待打款订单，批量打款到
        List<Map<String,Object>> orders =  cancelMoneyService.cancelOrders();
        if(Common.isNotEmpty(orders)){
            orders.forEach(x->{
                String money = Convert.objToStr(x.get("order_amount"));
                int uid = Convert.objToInt(x.get("uid"));
                LoginInfo loginInfo = loginService.info(uid);
                String openId = loginInfo.getOpenId();
//
//                WxWithdrawal wxWithdrawal = new WxWithdrawal();
//                wxWithdrawal.setIp("127.0.0.1");
//                wxWithdrawal.setOpenid(openId);
//                wxWithdrawal.setUid(String.valueOf(loginInfo.getId()));
//                wxWithdrawal.setUsername(loginInfo.getNickName());
//                wxWithdrawal.setCheckUser(false);
//
//                wxWithdrawal.setTotal_money(String.valueOf(money));
//                wxWithdrawal.setUser_money(String.valueOf(money));
//                wxWithdrawal.setRate_money("0");
//                String order_sn = MD5.md5(String.format("%s-%s-%s-%s-%s", uid, openId, money, System.currentTimeMillis(), StringHandler.getRandomStr(10)));
//                wxWithdrawal.setOrder_sn(order_sn);
//                wxWithdrawal.setRemark(String.format("uid:%s;退货金额:%s元,openid:%s", uid, money, openId));
//                wxWithdrawal.setPay_remark(String.format("添宝孕婴退货款项￥%s元", money));
//                wxWithdrawal.setStatus("0");//初始状态
//                wxWithdrawal.setType(2);
//                wxWithdrawal.setAppid(loginInfo.getAppId());
//                long id = wxService.savaUserBankWithdrawal(wxWithdrawal);
//                if(id>0){
//                    try {
//                        Map<String,Object> param = new HashMap<String,Object>(){{
//                            put("order_sn",x.get("order_sn"));
//                        }};
//                        wxService.withdrawals(wxWithdrawal, param);
//                    } catch (Throwable ex) {
//                        Logger.error("退货打款异常:微信异常码："+wxWithdrawal.getErr_code_des(),ex);
//                        ex.printStackTrace();
//                        if ("0".equals(wxWithdrawal.getStatus())) {
//                            wxWithdrawal.setStatus("4");
//                            wxWithdrawal.setResult(ex.getMessage());
//                        }
////                        if(wxWithdrawal.getErr_code_des().contains("今日付款总额上限")){
////                            long q = System.currentTimeMillis();
////                            long s = DateUtil.paseDatetime(DateUtil.getDate()+" 23:59:59").getTime();
////                            redisOperate.set("withdrawal_all_money_limit","1",new Long(s-q).intValue()/1000+100);
////                        }
//                    }
//                    wxService.updateUserBankWithdrawal(wxWithdrawal);
//                }
            });
        }
    }
}
